package servicesImp.expertInfo;


import StaticFinal.CrudType;
import base.BaseEntity;
import base.IBaseMapperWithoutBLOBs;
import base.IBaseServicesWithoutBLOBsImpl;
import exception.WTException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import services.dictionary.MajorTypeServices;
import services.expertInfo.ExpertBaseServices;
import services.expertInfo.ExpertMajorServices;
import services.expertInfo.LeaderCheckServices;
import wt.dao.expertInfo.ExpertMajorMapper;
import wt.entity.dictionary.MajorType;
import wt.entity.expertInfo.ExpertBase;
import wt.entity.expertInfo.ExpertMajor;
import wt.entity.expertInfo.ExpertMajorExample;
import wt.entity.expertInfo.LeaderCheckWithBLOBs;
import wt.staticData.TableName;
import wt.webPlugin.ZTree;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/14.
 */
@Service
public class ExpertMajorServicesImp extends IBaseServicesWithoutBLOBsImpl<ExpertMajor, ExpertMajorExample> implements ExpertMajorServices {
    @Autowired
    ExpertMajorMapper expertMajorMapper;
    @Autowired
    MajorTypeServices majorTypeServices;
    @Autowired
    LeaderCheckServices leaderCheckServices;
    @Autowired
    ExpertBaseServices expertBaseServices;

    @Override
    public IBaseMapperWithoutBLOBs<ExpertMajor, ExpertMajorExample> getMapper() {
        return this.expertMajorMapper;
    }

    @Override
    public String getTableName() {
        return TableName.EMS_EXPERT_MAJOR;
    }

    @Transactional
    public synchronized int expertUpdateMajor(List<String> addMajorTypeIdList, List<String> deleteMajorTypeIdList, String expertId) throws Exception {
        //是否有专家的修改记录
        if (isExistUnauditedRecord(expertId)) {
            throw new WTException("存在还未审核通过的记录");
        }
        int count = 0;
        if (addMajorTypeIdList != null) {
            ExpertMajor expertMajor = new ExpertMajor();
            expertMajor.setExpertId(expertId);
            expertMajor.setCrudType(CrudType.insert);
            for (int i = 0; i < addMajorTypeIdList.size(); i++) {
                expertMajor.setId(null);
                expertMajor.setMajorId(addMajorTypeIdList.get(i));
                count += insertSelective(expertMajor);
            }
        }

        if (deleteMajorTypeIdList != null) {
            ExpertMajorExample expertMajorExample = new ExpertMajorExample();
            ExpertMajor expertMajor = new ExpertMajor();
            expertMajor.setCrudType(CrudType.delete);
            for (int i = 0; i < deleteMajorTypeIdList.size(); i = i + 1000) {
                expertMajorExample.clear();
                expertMajorExample.createCriteria().andExpertIdEqualTo(expertId).andMajorIdIn(deleteMajorTypeIdList.subList(i, i + 1000 < deleteMajorTypeIdList.size() ? i + 1000 : deleteMajorTypeIdList.size()));
                count += getCurrentProxy().updateByExampleSelective(expertMajor, expertMajorExample);

            }
        }

        return count;

    }


    public List<ExpertMajor> expertSelectByExpertId(String expertId) throws Exception {
        ExpertMajorExample expertMajorExample = new ExpertMajorExample();
        expertMajorExample.createCriteria().andExpertIdEqualTo(expertId);
        List<ExpertMajor> expertMajorList = selectByExample(expertMajorExample);
        return expertMajorList;
    }


    @Transactional
    public int adminUpdateMajor(List<String> addMajorTypeIdList, List<String> deleteMajorTypeIdList, String expertId) throws Exception {

        if (isExistUnauditedRecord(expertId)) {
            throw new WTException(WTException.FOUND_NOT_CHECK_DATA);
        }

        int count = 0;
        ArrayList<String> arrayList = new ArrayList();//添加和删除的记录的集合
        int addSize = 0;

        if (addMajorTypeIdList != null) {
            arrayList.addAll(addMajorTypeIdList);
            addSize = arrayList.size();
            ExpertMajor expertMajor = new ExpertMajor();
            expertMajor.setExpertId(expertId);
            expertMajor.setCrudType(CrudType.select);
            for (int i = 0; i < addMajorTypeIdList.size(); i++) {
                expertMajor.setId(null);
                expertMajor.setMajorId(addMajorTypeIdList.get(i));
                count += insertSelective(expertMajor);//占时不记录日志
            }
        }

        if (deleteMajorTypeIdList != null) {
            arrayList.addAll(deleteMajorTypeIdList);
            ExpertMajorExample expertMajorExample = new ExpertMajorExample();
            for (int i = 0; i < deleteMajorTypeIdList.size(); i = i + 1000) {
                expertMajorExample.clear();
                expertMajorExample.createCriteria().andExpertIdEqualTo(expertId).andMajorIdIn(deleteMajorTypeIdList.subList(i, i + 1000 < deleteMajorTypeIdList.size() ? i + 1000 : deleteMajorTypeIdList.size()));
                count += deleteByExample(expertMajorExample);//占时不记录日志
            }
        }


        //领导复查
        if (!arrayList.isEmpty()) {
            List<MajorType> majorTypeList = majorTypeServices.selectTreeByLeafId(arrayList);
            JSONArray jsonArray = new JSONArray();
            for (MajorType majorType : majorTypeList) {
                JSONObject jsonObject = ZTree.createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getName());
                for (int i = 0; i < arrayList.size(); i++) {
                    String s = arrayList.get(i);
                    if (s.equals(majorType.getId())) {
                        if (i < addSize) {
                            jsonObject.put(ZTree.ICON_SKIN, "add");
                        } else {
                            jsonObject.put(ZTree.ICON_SKIN, "remove");
                        }
                    }
                }
                jsonArray.add(jsonObject);
            }
            ExpertBase expertBase = expertBaseServices.selectByPrimaryKey(expertId);

            LeaderCheckWithBLOBs blobs = LeaderCheckUtil.verifier1Update(null, jsonArray, getTableName(), expertBase);
            leaderCheckServices.insertSelective(blobs);
        }
        return count;
    }


    public List<MajorType> adminSelectForShow(String expertId) throws Exception {
        List<String> majorTypeIdList = adminSelectCheckedLeafIds(expertId);
        List<MajorType> majorTypeList = majorTypeServices.selectTreeByLeafId(majorTypeIdList);
        return majorTypeList;
    }


    public List<ExpertMajor> adminSelectByMajorNumList(List<String> majorNumList) throws Exception {
        List<MajorType> majorTypeList = majorTypeServices.selectByMajorNumList(majorNumList);
        List<String> majorIdList = BaseEntity.getIdList(majorTypeList);
        return adminSelectByMajorIdList(majorIdList);


    }

    public List<ExpertMajor> adminSelectByMajorIdList(List<String> majorIdList) throws Exception {
        if (majorIdList == null || majorIdList.isEmpty()) {
            return new ArrayList<ExpertMajor>();
        }

        ExpertMajorExample expertMajorExample = new ExpertMajorExample();
        for (int i = 0; i < majorIdList.size(); i += 1000) {
            expertMajorExample.or().andMajorIdIn(majorIdList.subList(i, majorIdList.size() <= i + 1000 ? majorIdList.size() : i + 1000));
        }
        List<ExpertMajor> expertMajorList = expertMajorMapper.selectByExample(expertMajorExample);
        return expertMajorList;
    }

    public List<String> adminSelectCheckedLeafIds(String expertId) throws Exception {
        ExpertMajorExample expertMajorExample = new ExpertMajorExample();
        expertMajorExample.createCriteria().andExpertIdEqualTo(expertId).andCrudTypeNotEqualTo(CrudType.insert);
        List<ExpertMajor> expertMajorList = selectByExample(expertMajorExample);
        List<String> majorTypeIdList = new ArrayList<String>(expertMajorList.size());
        for (ExpertMajor expertMajor : expertMajorList) {
            majorTypeIdList.add(expertMajor.getMajorId());
        }
        return majorTypeIdList;
    }

    public boolean isExistUnauditedRecord(String expertId) throws Exception {
        if (StringUtils.isEmpty(expertId)) {
            return false;
        }

        ExpertMajorExample expertMajorExample = new ExpertMajorExample();
        expertMajorExample.createCriteria().andExpertIdEqualTo(expertId).andCrudTypeNotEqualTo(CrudType.select);
        int count = countByExample(expertMajorExample);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Transactional
    public int clearUnauditedRecord(String expertId) throws Exception {
        int count = 0;
        if (isExistUnauditedRecord(expertId) == false) {
            return count;
        }
        ExpertMajorExample expertMajorExample = new ExpertMajorExample();
        //清除专家新增的
        expertMajorExample.createCriteria().andExpertIdEqualTo(expertId).andCrudTypeEqualTo(CrudType.insert);
        count += deleteByExample(expertMajorExample);

        expertMajorExample.clear();
        //清除专家删除的
        expertMajorExample.createCriteria().andExpertIdEqualTo(expertId).andCrudTypeEqualTo(CrudType.delete);
        ExpertMajor expertMajor = new ExpertMajor();
        expertMajor.setCrudType(CrudType.select);
        count += getCurrentProxy().updateByExampleSelective(expertMajor, expertMajorExample);
        return count;
    }


    @Transactional
    public void adminCheckApplyByPrimaryKey(String expertId, boolean result, String reason) throws Exception {
        ExpertMajorExample expertMajorExample = new ExpertMajorExample();
        expertMajorExample.createCriteria().andExpertIdEqualTo(expertId).andCrudTypeNotEqualTo(CrudType.select);
        List<ExpertMajor> expertMajorList = selectByExample(expertMajorExample);

        if (expertMajorList.size() == 0) {
            return;
        }

        List<String> stringList = new ArrayList<String>();
        for (ExpertMajor expertMajor : expertMajorList) {
            stringList.add(expertMajor.getMajorId());
        }
        List<MajorType> majorTypeList = majorTypeServices.selectTreeByLeafId(stringList);

        JSONArray jsonArray = new JSONArray();
        for (MajorType majorType : majorTypeList) {
            JSONObject jsonObject = ZTree.createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getName());
            for (ExpertMajor expertMajor : expertMajorList) {
                if (expertMajor.getMajorId().equals(majorType.getId())) {
                    if (expertMajor.getCrudType() == CrudType.insert) {
                        jsonObject.put(ZTree.ICON_SKIN, "add");
                    } else {
                        jsonObject.put(ZTree.ICON_SKIN, "remove");
                    }
                }
            }
            jsonArray.add(jsonObject);
        }


        if (result) {

            expertMajorExample.clear();
            expertMajorExample.createCriteria().andExpertIdEqualTo(expertId).andCrudTypeEqualTo(CrudType.insert);
            ExpertMajor temp = new ExpertMajor();
            temp.setCrudType(CrudType.select);
            getCurrentProxy().updateByExampleSelective(temp, expertMajorExample);

            expertMajorExample.clear();
            expertMajorExample.createCriteria().andExpertIdEqualTo(expertId).andCrudTypeEqualTo(CrudType.delete);
            getCurrentProxy().deleteByExample(expertMajorExample);

            ExpertBase expertBase = expertBaseServices.selectByPrimaryKey(expertId);

            LeaderCheckWithBLOBs blobs = LeaderCheckUtil.verifier1Update(null, jsonArray, getTableName(), expertBase);
            leaderCheckServices.insertSelective(blobs);

        } else {


            expertMajorExample.clear();
            expertMajorExample.createCriteria().andExpertIdEqualTo(expertId).andCrudTypeEqualTo(CrudType.insert);
            getCurrentProxy().deleteByExample(expertMajorExample);

            expertMajorExample.clear();
            expertMajorExample.createCriteria().andExpertIdEqualTo(expertId).andCrudTypeEqualTo(CrudType.delete);
            ExpertMajor expertMajor = new ExpertMajor();
            expertMajor.setCrudType(CrudType.select);
            getCurrentProxy().updateByExampleSelective(expertMajor, expertMajorExample);
            ExpertBase expertBase = expertBaseServices.selectByPrimaryKey(expertId);

            LeaderCheckWithBLOBs blobs = LeaderCheckUtil.verifier1UpdateDisagree(null, jsonArray, getTableName(), expertBase, reason);
            leaderCheckServices.insertSelective(blobs);

        }


    }
}
